export const initstate = {
    todos: [{
        id: 1,
        task: "任务一",
        flag: false
    }]
}

export const actions = {
    addTodos(payload) {
        console.log("payload", payload)
        // payload 负载
        // 如果没有进行数据请求，name我们只要创建动作
        return {
            type: 'ADD_TODOS',
            payload
        }
    },
    delTodos(payload) {
        return {
            type: "DEL_TODOS",
            payload
        }
    }
}

export function reducers (state = initstate, { type, payload }) {
    // 使用深拷贝，界面才能发生更新
    const newState = JSON.parse(JSON.stringify(state))
    switch(type) {
        case 'ADD_TODOS':
            newState.todos.push({
                id: newState.todos.length+1,
                task: payload,
                flag: false
            })
            break;
        case 'DEL_TODOS':
            newState.todos.splice(payload, 1)
            break;
        default: 
            return newState
            break;
    }
    return newState
}